package net.lab1024.sa.admin.module.business.erp.bas.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.bas.dao.ApptemordDao;
import net.lab1024.sa.admin.module.business.erp.bas.dao.ApptemordaDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.ApptemordEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.ApptemordaEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.ApptemordAddForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.ApptemordQueryForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.ApptemordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.vo.ApptemordVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 审批模板主表 Service
 *
 * @Author AKITA
 * @Date 2025-04-23 09:53:21
 * @Copyright AKITA
 */

@Service
public class ApptemordService {

    @Resource
    private ApptemordDao apptemordDao;
    @Resource
    private ApptemordaDao apptemordaDao;

    /**
     * 分页查询
     */
    public PageResult<ApptemordVO> queryPage(ApptemordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ApptemordVO> list = apptemordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */

    @Transactional
    public ResponseDTO<Long> add(ApptemordAddForm addForm) {
        ApptemordEntity apptemordEntity = SmartBeanUtil.copy(addForm, ApptemordEntity.class);
        apptemordDao.insert(apptemordEntity);

        return ResponseDTO.ok(apptemordEntity.getDocEntry());
    }

    /**
     * 更新
     */
    @Transactional
    public ResponseDTO<String> update(ApptemordUpdateForm updateForm) {
        ApptemordEntity apptemordEntity = SmartBeanUtil.copy(updateForm, ApptemordEntity.class);
        if ( apptemordDao.updateById(apptemordEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        apptemordaDao.deleteById(updateForm.getDocEntry());
        for (ApptemordaEntity apptemordaEntity : updateForm.getLista()) {
            apptemordaDao.insert(apptemordaEntity);
        }

        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        apptemordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */

    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        if (null == docEntry) {
            return ResponseDTO.ok();
        }

        apptemordDao.deleteById(docEntry);
        apptemordaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<ApptemordEntity> info(Long docEntry) {
        ApptemordEntity apptemordEntity = apptemordDao.selectById(docEntry);
        apptemordEntity.setLista(apptemordaDao.selectList(new LambdaQueryWrapper<ApptemordaEntity>().eq(true, ApptemordaEntity::getDocEntry, docEntry)));
        return ResponseDTO.ok(apptemordEntity);

    }

    public ApptemordEntity infoByTabId(String tabName) {
        ApptemordEntity apptemordEntity = apptemordDao.selectOne(new LambdaQueryWrapper<ApptemordEntity>().eq(true, ApptemordEntity::getTabName, tabName));
        if (null == apptemordEntity) {
            throw new RuntimeException("审批模板未生成！");
        }
        apptemordEntity.setLista(apptemordaDao.selectList(new LambdaQueryWrapper<ApptemordaEntity>().eq(true, ApptemordaEntity::getDocEntry, apptemordEntity.getDocEntry())));
        return apptemordEntity;
    }
}
